package net.zjitc.controller.admin;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @Author admin
 * @Date 2019/4/10 10:28
 */
@Controller
public class DownloadController {
    @RequestMapping("/download")
    public void download(String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //通过request拿到项目的路径
        String path= request.getServletContext().getRealPath("/WEB-INF/");
        File file=new File(path,fileName);
        System.out.println(file);
        //将文件输入出前端IO
        //读取文件内容Inputstream，输出outputstream
        //FileInputStream inputstream=new FileInputStream(file)
        //必须告诉下载
        //中国乱码问题
        String string = new String("文件名字.jpg".getBytes(), "iso-8859-1");
        response.addHeader("content-disposition","attachment;filename="+string);
        IOUtils.copy(new FileInputStream(file),response.getOutputStream());


    }
}
